package com.luocc.tim.ui;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.luocc.tim.MainActivity;
import com.luocc.tim.R;

import java.util.HashMap;
import java.util.Map;

public class MainFragment extends Fragment implements View.OnClickListener {
    public static final String TAG = "MainFragment";
    private static final Map<String, Fragment> mFragmentMap = new HashMap<>();

    private MainActivity activity;
    private Button message;
    private Button contacts;
    private Button personal;
    private Button lastClick;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (MainActivity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        WindowInsetsControllerCompat windowInsetsController = ViewCompat.getWindowInsetsController(view);
        assert windowInsetsController != null;
        windowInsetsController.setAppearanceLightStatusBars(true);
        switchToFragment("message");
        message = view.findViewById(R.id.button_message);
        contacts = view.findViewById(R.id.button_contacts);
        personal = view.findViewById(R.id.button_personal);
        lastClick = message;
        message.setOnClickListener(this);
        // 默认是message
        message.setTextColor(activity.getColor(R.color.blue));
        message.setTextSize(30);
        contacts.setOnClickListener(this);
        personal.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        lastClick.setTextSize(20);
        lastClick.setTextColor(activity.getColor(R.color.black));
        if (v == message) {
            switchToFragment("Message");
            message.setTextColor(activity.getColor(R.color.blue));
            message.setTextSize(30);
            lastClick = message;
        } else if (v == contacts) {
            switchToFragment("Contacts");
            contacts.setTextColor(activity.getColor(R.color.blue));
            contacts.setTextSize(30);
            lastClick = contacts;
        } else if (v == personal) {
            switchToFragment("Personal");
            personal.setTextColor(activity.getColor(R.color.blue));
            personal.setTextSize(30);
            lastClick = personal;
        }
    }

    private void switchToFragment(String name) {
        Fragment fragment = null;
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        for (Map.Entry<String, Fragment> entry : mFragmentMap.entrySet()) {
            if (name.equals(entry.getKey())) {
                fragment = entry.getValue();
            } else {
                transaction.hide(entry.getValue());
            }
        }
        if (fragment == null) {
            fragment = createFragment(name);
            mFragmentMap.put(name, fragment);
        }
        Log.d(TAG, "switchToFragment: " + fragment.getClass().getSimpleName());

        if (!fragment.isAdded()) {
            transaction.add(R.id.main_fragment_container, fragment, "SECOND_FRAGMENT");
        }
        transaction.show(fragment);
        transaction.commit();
    }

    private Fragment createFragment(String name) {
        Fragment fragment;
        switch (name) {
            case "Contacts":
                fragment = new ContactsFragment();
                break;
            case "Personal":
                fragment = new PersonalFragment();
                break;
            case "Message":
            default:
                fragment = new ChatListFragment();
                break;
        }
        return fragment;
    }
}
